package com.ibm.productivity.tools.blogplugin;

import java.util.Iterator;
import java.util.LinkedList;

import com.ibm.productivity.tools.blogplugin.meta.Article;
import com.ibm.productivity.tools.blogplugin.meta.Comment;
import com.ibm.productivity.tools.ui.views.RichDocumentView;

public class EditingViewList {
	LinkedList<EditingView> list = new LinkedList<EditingView>();
	LinkedList<EditingView> commentsList = new LinkedList<EditingView>();

	public void add(Article entry, RichDocumentView viewer) {
		EditingView node = new EditingView(entry, viewer);
		list.add(node);
	}

	public void remove(Article a) {
		int pos = 0;
		Iterator<EditingView> it = list.iterator();
		while (it.hasNext()) {
			EditingView node = it.next();
			if (node.article == a) {
				list.remove(pos);
				return;
			}
			pos++;
		}

		
	}
	
	public void remove(Comment a) {
		int pos = 0;
		Iterator<EditingView> it;
		it = commentsList.iterator();
		pos = 0;
		while (it.hasNext()) {
			EditingView node = it.next();
			if (node.commment == a) {
				commentsList.remove(pos);
				return;
			}
			pos++;
		}
	}
	
	public void remove(RichDocumentView view) {
		int pos = 0;
		Iterator<EditingView> it = list.iterator();
		while (it.hasNext()) {
			EditingView node = it.next();
			if (node.viewer == view) {
				list.remove(pos);
				return;
			}
			pos++;
		}

		it = commentsList.iterator();
		pos = 0;
		while (it.hasNext()) {
			EditingView node = it.next();
			if (node.viewer == view) {
				commentsList.remove(pos);
				return;
			}
			pos++;
		}
	}
	
	public void addComments(Comment comment, RichDocumentView viewer) {
		EditingView node = new EditingView(comment, viewer);
		commentsList.add(node);
	}

	public Article getArticle(RichDocumentView viewer) {
		Iterator<EditingView> it = list.iterator();
		while (it.hasNext()) {
			EditingView node = it.next();
			if (node.viewer == viewer)
				return node.article;
		}
		return null;
	}

	public boolean isNewView(RichDocumentView viewer) {
		if (getArticle(viewer) == null)
			return true;
		return false;
	}

	public boolean isNewView(Article entry) {
		Iterator<EditingView> it = list.iterator();
		while (it.hasNext()) {
			EditingView node = it.next();
			if (node.article == entry)
				return false;
		}
		return true;
	}

	public boolean isCommentView(RichDocumentView viewer) {
		Iterator<EditingView> it = commentsList.iterator();
		while (it.hasNext()) {
			EditingView node = it.next();
			if (node.viewer == viewer)
				return true;
		}
		return false;
	}

	public boolean isNewCommentView(Comment comment) {
		Iterator<EditingView> it = commentsList.iterator();
		while (it.hasNext()) {
			EditingView node = it.next();
			if (node.commment == comment)
				return false;
		}
		return true;
	}
}
